package com.ayf.payment.game.api.sms;

import com.alibaba.fastjson.JSONObject;
import com.ayf.payment.game.api.entity.platform.SmsChannelConfig;
import com.ayf.payment.game.api.entity.platform.SmsRecord;
import com.ayf.payment.game.api.httpclient.HttpUtils;
import com.ayf.payment.game.api.type.SMSType;
import com.ayf.payment.game.api.utils.RandomStrUtils;
import com.ayf.payment.game.api.utils.SnowFlakeUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @Description: 游戏宝短信
 * @Author: zh-liang
 * @Date : 2020-04-13 15:30
 */
@Component
public class YouXiBaoSMSHandler extends SMSHandler {

    public void send(String phone, SMSType type, SmsChannelConfig sMSConfig, Integer merchantId) {
        String traceId = SnowFlakeUtils.getTraceId();
        JSONObject config = JSONObject.parseObject(sMSConfig.getChannelConfig());
        String code = RandomStrUtils.createRandom(true, 6);
        String url = config.getString("url");
        String data = "phone=" + phone + "&code=" + code;
        try {
            Map<String, String> map = HttpUtils.doGet(traceId, url + "?" + data);
            String date = map.get(HttpUtils.RESPONSE_DATA);
            SmsRecord smsRecord = new SmsRecord();
            if (map.get(HttpUtils.RESPONSE_CODE).equals("200")) {
                switch (date) {
                    case "0":
                        smsRecord.setStatus(1);
                        LogPortal.info("[{}]游戏宝短信验证码发送成功,发送手机号:[{}],参数:[{}],返回结果:[{}]", traceId, phone, data, map);
                        break;
                    case "40":
                        smsRecord.setStatus(0);
                        smsRecord.setResponseCodeMsg("收不到验证码");
                        LogPortal.error("[{}]游戏宝短信验证码发送失败,发送手机号:[{}]发送内容为:[{}],错误码为:[40]原因为:[{}]", traceId, phone, code);
                        break;
                    case "15":
                        smsRecord.setStatus(0);
                        smsRecord.setResponseCodeMsg("手机格式不正确");
                        LogPortal.error("[{}]游戏宝短信验证码发送失败,发送手机号:[{}]发送内容为:[{}],错误码为:[15],原因为:[手机格式不正确]", traceId,
                                phone, code);
                        break;
                    default:
                        smsRecord.setStatus(0);
                        LogPortal.error("[{}]游戏宝短信验证码发送失败,发送手机号:[{}]发送内容为:[{}]", traceId, phone, code);
                }
            } else {
                smsRecord.setStatus(0);
                smsRecord.setResponseCodeMsg("请求游戏宝短信接口失败");
                LogPortal.error("[{}]游戏宝短信验证码发送失败,发送手机号:[{}]发送内容为:[{}],调用接口失败", traceId, phone, code);
            }
            smsRecord.setSendPhone(phone);
            smsRecord.setSmsChannel(sMSConfig.getChannelName());
            smsRecord.setSendContent(code);
            smsRecord.setResponseCode(date);
            save(smsRecord, type, code, merchantId);
        } catch (IOException e) {
            LogPortal.error("[{}]游戏宝短信验证码发送失败,发送手机号:[{}]发送内容为:[{}],异常为[{}]", traceId, phone, code, e.getMessage());
        }
    }
}
